﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BrewTarget.Models
{
    public class Equipment : BeerBase
    {

        public string Name { get; set; }

        public double BoilSizeInLiters { get; set; }


        private double p_BatchSizeInLiters;
        public double BatchSizeInLiters
        {
            get { return p_BatchSizeInLiters; }
            set
            {
                if (value < 0.0)
                    return;

                p_BatchSizeInLiters = value;
                DoCalculations();
            }
        }

        public double TunVolumeInLiters { get; set; }


        public double TunWeightInKiloGrams { get; set; }


        public double TunSpecificHeat_calGC { get; set; }


        public double TopUpWeightInLiters { get; set; }


        private double p_TrubChillerLossInLiters = 0;
        public double TrubChillerLossInLiters
        {
            get { return p_TrubChillerLossInLiters; }
            set
            {
                if (value < 0.0)
                    return;

                p_TrubChillerLossInLiters = value;
                DoCalculations();
            }
        }

        private double p_DONOTUSE_EvaporationRate_pctHr = 0;
        public double DONOTUSE_EvaporationRate_pctHr
        {
            get { return p_DONOTUSE_EvaporationRate_pctHr; }
            set
            {
                if ((value < 0.0) || (value > 100.0))
                    return;

                p_DONOTUSE_EvaporationRate_pctHr = value;
                p_EvaporationRate_LitersHr = (value / 100) * BatchSizeInLiters;
                DoCalculations();
            }
        }

        private double p_EvaporationRate_LitersHr = 0;
        public double EvaporationRate_LitersHr
        {
            get { return p_EvaporationRate_LitersHr; }
            set
            {
                if ((value < 0.0) || (value > 100.0))
                    return;

                p_EvaporationRate_LitersHr = value;
                p_DONOTUSE_EvaporationRate_pctHr = (value / BatchSizeInLiters) * 100;
                DoCalculations();
            }
        }

        private TimeSpan p_BoilTime = TimeSpan.Zero;
        public TimeSpan BoilTime
        {
            get { return p_BoilTime; }
            set
            {
                if (value < TimeSpan.Zero)
                    return;

                p_BoilTime = value;
                DoCalculations();
            }
        }

        private bool p_CaluclateBoilVolume = false;
        public bool CaluclateBoilVolume
        {
            get { return p_CaluclateBoilVolume; }
            set
            {
                p_CaluclateBoilVolume = value;
                if (value)
                    DoCalculations();

            }
        }


        public double LauterTunDeadSpaceInLiters { get; set; }


        public double KettleTopUpInLiters { get; set; }

        public double HopUtilization_Pct { get; set; }


        public string Notes { get; set; }


        public double GrainAbsorptionInLitersPerKilogram { get; set; }

        public double BoilingPointInCelsius { get; set; }


        public double WortEndOfBoil(double inKettleWort)
        {
            return inKettleWort - (BoilTime.TotalHours * EvaporationRate_LitersHr);
        }

        public void DoCalculations()
        {
            // Only do the calculation if we're asked to.
            if (!CaluclateBoilVolume)
                return;
            BoilSizeInLiters = BatchSizeInLiters - TopUpWeightInLiters + TrubChillerLossInLiters + (BoilTime.TotalHours * EvaporationRate_LitersHr);
        }
    }
}
